package xin.it.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import xin.it.bean.QueryPageParam;
import xin.it.bean.Result;
import xin.it.bean.User;
import xin.it.dto.UserReaname;
import xin.it.mapper.UserMapper;
import xin.it.service.UserService;
import xin.it.service.impl.UserServiceImpl;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhenhui
 * @since 2023-03-18
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @GetMapping("/index")
    public Result index() {
        User byId = userService.getById(1L);
        return Result.suc(byId);
    }

    //查询所有
    @PostMapping("/list")
    public Result list() {
        List<User> list = userService.listAll();
        return Result.suc(list);
    }

    //查询某一字段的全部值
    @GetMapping("loginId")
    public Result listloginId(@RequestParam String loginid) {
        //两种写法
       /* QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("loginId",loginid);//相当于where id=1
        List<User> list = userMapper.selectList(wrapper);*/
        List list = userService.lambdaQuery().eq(User::getLoginId,loginid).list();
        return list.size()>0?Result.suc(list):Result.fall();
    }

     //新增
     @RequestMapping("/save")
     public Result save(@RequestBody User user) {
         return userService.save(user)?Result.suc():Result.fall();
     }
     //修改
     @RequestMapping("/mod")
     public boolean mod(@RequestBody User user) {
         return userService.updateById(user);
     }
     //更新
     @RequestMapping("/update")
     public Result update(@RequestBody User user) {
         return userService.updateById(user)?Result.suc():Result.fall();
     }
     //新增或修改
     @RequestMapping("/saveOrMod")
     public boolean saveOrMod(@RequestBody User user) {
         return userService.saveOrUpdate(user);
     }
     //删除
     @GetMapping("/delete")
     public Result delete(Integer id) {
         boolean i = userService.removeById(id);
         System.out.println(i);
         return i==true?Result.suc(i):Result.fall();
     }
    //查询（模糊查询、匹配）
    @RequestMapping("/listP")
    public Result listp(@RequestBody User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(user.getReaName())){
        lambdaQueryWrapper.like(User::getReaName, user.getReaName());}
        return Result.suc(userService.listAll());
    }
//分页功能
    @RequestMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = (String) param.get("name");
        System.out.println("name=="+(String) param.get("name"));
        Page<User> page = new Page();
        page.setCurrent(query.getPagenum());
        page.setSize(query.getPagesize());
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.eq(User::getReaName, name);
        }
        IPage result = userService.page(page, lambdaQueryWrapper);

        System.out.println("总条数：" + result.getTotal());

        return Result.suc(result.getRecords(), result.getTotal());

    }
}

